<template>
  <div>
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline" @keyup.enter="onSubmit">
      <el-form-item label="创建时间">
      <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始时间"></el-date-picker>
       —
      <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束时间"></el-date-picker>
      </el-form-item>
        <!-- <el-form-item label="编号">
         <el-input v-model="searchInfo.number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="制造序列号">
         <el-input v-model="searchInfo.manufacturing_serial_number" placeholder="搜索条件" />

        </el-form-item> -->
        <!-- <el-form-item label="排故日期">
            
           <el-date-picker v-model="searchInfo.troubleshooting_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item> -->
        <!-- <el-form-item label="故障对应的工艺设计文件号">
         <el-input v-model="searchInfo.process_design_document_number" placeholder="搜索条件" />

        </el-form-item> -->
        <el-form-item label="负责工艺员">
         <el-input v-model="searchInfo.responsible_craftman" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障描述">
         <el-input v-model="searchInfo.fault_description" placeholder="搜索条件" />

        </el-form-item>
        <!-- <el-form-item label="DISR号">
         <el-input v-model="searchInfo.dISR_number" placeholder="搜索条件" />

        </el-form-item> -->
        <!-- <el-form-item label="操作描述1">
         <el-input v-model="searchInfo.operation_description1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述2">
         <el-input v-model="searchInfo.operation_description2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述3">
         <el-input v-model="searchInfo.operation_description3" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述4">
         <el-input v-model="searchInfo.operation_description4" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述5">
         <el-input v-model="searchInfo.operation_description5" placeholder="搜索条件" />

        </el-form-item> -->
        <el-form-item label="ATA章节（一级）">
         <el-input v-model="searchInfo.ata_chapter_level1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（二级）">
         <el-input v-model="searchInfo.ata_chapter_level2" placeholder="搜索条件" />

        </el-form-item>
        <!-- <el-form-item label="结论">
         <el-input v-model="searchInfo.conclusion" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="完成时间">
            
           <el-date-picker v-model="searchInfo.complete_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
        <div class="gva-btn-list">
            <el-button type="primary" icon="plus" @click="AddForm">新增</el-button>
            <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="deleteVisible = false">取消</el-button>
                <el-button type="primary" @click="onDelete">确定</el-button>
            </div>
            <template #reference>
                <el-button icon="delete" style="margin-left: 10px;" :disabled="!multipleSelection.length" @click="deleteVisible = true">删除</el-button>
            </template>
            </el-popover>
        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        @selection-change="handleSelectionChange"
        >
        <el-table-column type="selection" width="55" />
        <el-table-column align="left" label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="编号" prop="number" width="150" />
        <!-- <el-table-column align="left" label="制造序列号" prop="manufacturing_serial_number" width="120" /> -->
         <!-- <el-table-column align="left" label="排故日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.troubleshooting_date) }}</template>
         </el-table-column> -->
        <!-- <el-table-column align="left" label="故障对应的工艺设计文件号" prop="process_design_document_number" width="120" /> -->
        <el-table-column align="left" label="负责工艺员" prop="responsible_craftman" width="120" />
        <el-table-column align="left" label="故障描述" prop="fault_description" width="120" />
        <!-- <el-table-column align="left" label="DISR号" prop="dISR_number" width="120" />
        <el-table-column align="left" label="序号1" prop="serial_number1" width="120" />
        <el-table-column align="left" label="操作描述1" prop="operation_description1" width="120" />
        <el-table-column align="left" label="是否需要质量检查1" prop="is_quality_inspection_required1" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required1) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号2" prop="serial_number2" width="120" />
        <el-table-column align="left" label="操作描述2" prop="operation_description2" width="120" />
        <el-table-column align="left" label="是否需要质量测查2" prop="is_quality_inspection_required2" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required2) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号3" prop="serial_number3" width="120" />
        <el-table-column align="left" label="操作描述3" prop="operation_description3" width="120" />
        <el-table-column align="left" label="是否需要质量检测3" prop="is_quality_inspection_required3" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required3) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号4" prop="serial_number4" width="120" />
        <el-table-column align="left" label="操作描述4" prop="operation_description4" width="120" />
        <el-table-column align="left" label="是否需要质量检查4" prop="is_quality_inspection_required4" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required4) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号5" prop="serial_number5" width="120" />
        <el-table-column align="left" label="操作描述5" prop="operation_description5" width="120" />
        <el-table-column align="left" label="是否需要质量检测5" prop="is_quality_inspection_required5" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required5) }}</template>
        </el-table-column> -->
        <el-table-column align="left" label="ATA章节（一级）" prop="ata_chapter_level1" width="150" />
        <el-table-column align="left" label="ATA章节（二级）" prop="ata_chapter_level2" width="150" />
        <!-- <el-table-column align="left" label="纸质排故单上传" prop="upload_troubleshooting_form" width="120" />
        <el-table-column align="left" label="附件上传" prop="upload_attachment" width="120" />
        <el-table-column align="left" label="结论" prop="conclusion" width="120" />
         <el-table-column align="left" label="完成时间" width="180">
            <template #default="scope">{{ formatDate(scope.row.complete_date) }}</template>
         </el-table-column> -->
        <el-table-column align="left" label="是否通过" prop="is_passed" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_passed) }}</template>
        </el-table-column>
        <el-table-column align="left" label="操作">
            <template #default="scope">
            <el-button type="primary" link icon="edit" class="table-button" @click="updateCase_structFunc(scope.row)">变更</el-button>
            <el-button type="primary" link icon="delete" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'Case_struct'
}
</script>

<script setup>
import {
  createCase_struct,
  deleteCase_struct,
  deleteCase_structByIds,
  updateCase_struct,
  findCase_struct,
  getCase_structList
} from '@/api/sysCaseLibrary'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
        number: '',
        manufacturing_serial_number: '',
        troubleshooting_date: new Date(),
        process_design_document_number: '',
        responsible_craftman: '',
        fault_description: '',
        dISR_number: '',
        serial_number1: 0,
        operation_description1: '',
        is_quality_inspection_required1: false,
        serial_number2: 0,
        operation_description2: '',
        is_quality_inspection_required2: false,
        serial_number3: 0,
        operation_description3: '',
        is_quality_inspection_required3: false,
        serial_number4: 0,
        operation_description4: '',
        is_quality_inspection_required4: false,
        serial_number5: 0,
        operation_description5: '',
        is_quality_inspection_required5: false,
        ata_chapter_level1: '',
        ata_chapter_level2: '',
        upload_troubleshooting_form: '',
        upload_attachment: '',
        conclusion: '',
        complete_date: new Date(),
        is_passed: false,
        })

// 验证规则
const rule = reactive({
               manufacturing_serial_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               troubleshooting_date : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               process_design_document_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               responsible_craftman : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               fault_description : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level1 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level2 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()


// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  if (searchInfo.value.is_quality_inspection_required1 === ""){
      searchInfo.value.is_quality_inspection_required1=null
  }
  if (searchInfo.value.is_quality_inspection_required2 === ""){
      searchInfo.value.is_quality_inspection_required2=null
  }
  if (searchInfo.value.is_quality_inspection_required3 === ""){
      searchInfo.value.is_quality_inspection_required3=null
  }
  if (searchInfo.value.is_quality_inspection_required4 === ""){
      searchInfo.value.is_quality_inspection_required4=null
  }
  if (searchInfo.value.is_quality_inspection_required5 === ""){
      searchInfo.value.is_quality_inspection_required5=null
  }
  if (searchInfo.value.is_passed === ""){
      searchInfo.value.is_passed=null
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getCase_structList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () =>{
}

// 获取需要的字典 可能为空 按需保留
setOptions()


// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

// 删除行
const deleteRow = (row) => {
    ElMessageBox.confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
            deleteCase_structFunc(row)
        })
    }


// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async() => {
      const ids = []
      if (multipleSelection.value.length === 0) {
        ElMessage({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      multipleSelection.value &&
        multipleSelection.value.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteCase_structByIds({ ids })
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        if (tableData.value.length === ids.length && page.value > 1) {
          page.value--
        }
        deleteVisible.value = false
        getTableData()
      }
    }

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateCase_structFunc = async(row) => {
    router.push({
      name:"case_formNew",
      query:{
        id:row.ID
      }
    })
  
    // const url = `#/layout/案例库/case_formNew?id=${row.ID}`
    // window.location.href = url


    // const res = await findCase_struct({ ID: row.ID })
    // type.value = 'update'
    // if (res.code === 0) {
    //     formData.value = res.data.recase_
    //     dialogFormVisible.value = true
    // }
}


// 删除行
const deleteCase_structFunc = async (row) => {
    const res = await deleteCase_struct({ ID: row.ID })
    if (res.code === 0) {
        ElMessage({
                type: 'success',
                message: '删除成功'
            })
            if (tableData.value.length === 1 && page.value > 1) {
            page.value--
        }
        getTableData()
    }
}


// 打开弹窗
const AddForm = () => {
    // type.value = 'create'
    // dialogFormVisible.value = true
    router.push({
      name:"case_formNew"
    })
}


</script>

<style lang="scss">
.el-input-number.full-width-input,
.el-cascader.full-width-input {
  width: 100% !important;
}

.el-form-item--medium {
  .el-radio {
    line-height: 36px !important;
  }
  
  .el-rate {
    margin-top: 8px;
  }
}

.el-form-item--small {
  .el-radio {
    line-height: 32px !important;
  }
  
  .el-rate {
    margin-top: 6px;
  }
}

.el-form-item--mini {
  .el-radio {
    line-height: 28px !important;
  }
  
  .el-rate {
    margin-top: 4px;
  }
}

.clear-fix:before,
.clear-fix:after {
  display: table;
  content: "";
}

.clear-fix:after {
  clear: both;
}

.float-right {
  float: right;
}

</style>

<style lang="scss" scoped>
div.table-container {
  table.table-layout {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;

    td.table-cell {
      display: table-cell;
      height: 36px;
      border: 1px solid #e1e2e3;
    }
  }
}

div.tab-container {}

.label-left-align ::v-deep .el-form-item__label {
  text-align: left;
}

.label-center-align ::v-deep .el-form-item__label {
  text-align: center;
}

.label-right-align ::v-deep .el-form-item__label {
  text-align: right;
}

.custom-label {}

.static-content-item {
  min-height: 20px;
  display: flex;
  align-items: center;

  ::v-deep .el-divider--horizontal {
    margin: 0;
  }
}

</style>
